/*
 * @Author: Laputa
 * @Version: V0.1
 * @Date: 2023-06-05 14:53:49
 * @LastEditors: Laputa
 * @LastEditTime: 2023-09-06 10:36:29
 * @Description: This file contains the functions prototypes for the EPORT
 * firmware library.
 *
 * Copyright (c) 2023 by Levetop, All Rights Reserved.
 */

#ifndef _LT168_EPORT_H
#define _LT168_EPORT_H

#include "LT168.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

#define EPORT_PIN_0_MASK   (0x01U)
#define EPORT_PIN_0_BIT    (0U)

#define EPORT_PIN_1_MASK   (0x02U)
#define EPORT_PIN_1_BIT    (1U)

#define EPORT_PIN_2_MASK   (0x04U)
#define EPORT_PIN_2_BIT    (2U)

#define EPORT_PIN_3_MASK   (0x08U)
#define EPORT_PIN_3_BIT    (3U)

#define EPORT_PIN_4_MASK   (0x10U)
#define EPORT_PIN_4_BIT    (4U)

#define EPORT_PIN_5_MASK   (0x20U)
#define EPORT_PIN_5_BIT    (5U)

#define EPORT_PIN_6_MASK   (0x40U)
#define EPORT_PIN_6_BIT    (6U)

#define EPORT_PIN_7_MASK   (0x80U)
#define EPORT_PIN_7_BIT    (7U)

/**
 * @brief: EPORT Pin Num Enumeration
 */
typedef enum
{
  EPORT_PIN_0 = EPORT_PIN_0_MASK,  /*!< pin 0 */
  EPORT_PIN_1 = EPORT_PIN_1_MASK,  /*!< pin 1 */
  EPORT_PIN_2 = EPORT_PIN_2_MASK,  /*!< pin 2 */
  EPORT_PIN_3 = EPORT_PIN_3_MASK,  /*!< pin 3 */
  EPORT_PIN_4 = EPORT_PIN_4_MASK,  /*!< pin 4 */
  EPORT_PIN_5 = EPORT_PIN_5_MASK,  /*!< pin 5 */
  EPORT_PIN_6 = EPORT_PIN_6_MASK,  /*!< pin 6 */
  EPORT_PIN_7 = EPORT_PIN_7_MASK,  /*!< pin 7 */
} eport_pin_te;

/**
 * @brief: EPORT Pin Direction Enumeration
 */
typedef enum
{
  EPORT_INPUT  = 0,  /*!< pin input mode */
  EPORT_OUTPUT = 1   /*!< pin output mode */
} eport_mode_te;

/**
 * @brief: EPORT Pin PullUp Enumeration
 */
typedef enum
{
  EPORT_No_Pull_up = 0,   /*!< pin disable pull up */
  EPORT_Pull_up    = 1    /*!< pin enable pull up */
} eport_pullup_te;

/**
 * @brief: EPORT Pin Output Mode Enumeration
 */
typedef enum
{
  EPORT_CMOS      = 0,    /*!< pin CMOS output */
  EPORT_OpenDrain = 1     /*!< pin Open Drain output */
} eport_outmode_te;

/**
 * @brief: EPORT Pin Level Sensitive Enumeration
 */
typedef enum
{
  EPORT_Level_LOW  = 0,   /*!< pin low level sensitive */
  EPORT_Level_HIGH = 1,   /*!< pin high level sensitive*/
} eport_level_sensitive_te;

/**
 * @brief: EPORT Pin Interrupt Type Enumeration
 */
typedef enum
{
  EPORT_Level               = 0,    /*!< pin interrupt level sensitive */
  EPORT_Rising_Edge         = 1,    /*!< pin interrupt rising edge triggered */
  EPORT_Falling_Edge        = 2,    /*!< pin interrupt falling edge triggered */
  EPORT_Rising_Falling_Edge = 3,    /*!< pin interrupt both falling edge and rising edge triggered */
} eport_interrupt_mode_te;

/**
 * @brief: EPORT Configuration Structure Definition
 */

typedef struct
{
  eport_pin_te   pin;            /*!< pin num */
  eport_mode_te  mode;           /*!< pin direction*/
  eport_outmode_te output_mode;  /*!< pin output mode */
  eport_pullup_te pullup;        /*!< pin pull up */

  /*< Interrupt */ 
  uint8_t int_enable;                    /*!< pin interrupt enable flag */
  eport_interrupt_mode_te int_type;      /*!< pin interrupt type */
  eport_level_sensitive_te level;        /*!< pin level sensitive type */

  /*< Digital Filter */
  uint8_t filter_enable;        /*!< pin digital filter enable flag */
  uint8_t filter_value;         /*!< pin digital filter value */

} eport_config_ts;
/*!<---------------End of Definitions--------------->!*/

/*******************************************************************************
 * APIs
 ******************************************************************************/

/*< EPORT Initialization Function */
void EPORT_DeInit(eport *eportx, eport_pin_te pin);
void EPORT_Init(eport *eportx, eport_config_ts *eport_config);

/*< EPORT Read/Write Operation Function */
/**
 * @brief: Write Pin Output status
 * @param {eport *} eportx: Pointer to a eport structure,where can be EPORT0,EPORT1,EPORT2
 * @param {eport_pin_te} pin: The pin of EPORT peripheral group,where can be EPORT_PIN_1 EPORT_PIN_2 ... EPORT_PIN_7.
 * @param {BitStatus} status: Pin Status,where can be RESET, SET.
 * @return {void}
 */
inline void EPORT_WritePin(eport *eportx, eport_pin_te pin, BitStatus status)
{
   if (status == Reset)
      eportx->EPBCR = pin;
  else
      eportx->EPBSR = pin; 
}

/**
 * @brief: Read Pin input status
 * @param {eport *} eportx: Pointer to a eport structure,where can be EPORT0,EPORT1,EPORT2
 * @param {eport_pin_te} pin: The pin of EPORT peripheral group,where can be EPORT_PIN_1 EPORT_PIN_2 ... EPORT_PIN_7.
 * @return {BitStatus} Return value can be RESET,SET.
 */
inline BitStatus EPORT_ReadPin(eport *eportx, eport_pin_te pin)
{
    if ((eportx->EPPDR & pin) == pin)
        return Set;
    else
        return Reset;
}

/*  EPORT Pin Interrupt Function */
void EPORT_Enable_Interrupt(eport *eportx, eport_pin_te pin);
void EPORT_Disable_Interrupt(eport *eportx, eport_pin_te pin);
void EPORT_Clear_IntFlag(eport *eportx, eport_pin_te pin);

/*!<---------------End of APIs--------------->!*/

#endif
/*<---------------End of _LT168_EPORT_H --------------->!*/
